package com.atguigu.spzx.manager.service.impl;

import com.atguigu.spzx.manager.mapper.SysUserRoleMapper;
import com.atguigu.spzx.manager.service.SysUserRoleService;
import jakarta.annotation.Resource;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.util.CollectionUtils;

import java.util.List;

@Service
public class SysUserRoleServiceImpl implements SysUserRoleService {
    @Resource
    SysUserRoleMapper sysUserRoleMapper;
    @Transactional(rollbackFor = Exception.class)
    @Override
    public void aggignRoles2User(Long userId, List<Long> roleIds) {
        //1、先删除userId所有的角色列表：
        sysUserRoleMapper.deleteAllRolesByUserId(userId);
        //2、再保存新分配的所有的角色信息
        //如果传入的角色id列表为空 不需要分配角色
        if(!CollectionUtils.isEmpty(roleIds)){
            sysUserRoleMapper.saveUserRoles(userId , roleIds);
        }
    }
}
